package com.xiaoxu.mp.controller;

import cn.hutool.core.bean.BeanUtil;
import com.xiaoxu.mp.domain.dto.PageDTO;
import com.xiaoxu.mp.domain.dto.UserFormDTO;
import com.xiaoxu.mp.domain.po.User;
import com.xiaoxu.mp.domain.query.UserQuery;
import com.xiaoxu.mp.domain.vo.UserVO;
import com.xiaoxu.mp.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "用户管理接口")
@RequestMapping("/users")
@RestController
//@AllArgsConstructor //所有的成员都构造
@RequiredArgsConstructor //必要的成员变量注入
public class UserController {


    private final IUserService userService;



    @ApiOperation("新增用户接口")
    @PostMapping
    public void saveUser(@RequestBody UserFormDTO userDTO){
        //1.把DTO拷贝到PO
        User user = BeanUtil.copyProperties(userDTO, User.class);
        //2.新增
        userService.save(user);
    }

    @ApiOperation("删除用户接口")
    @DeleteMapping("{id}")
    public void deleteUserById(@ApiParam("用户id") @PathVariable Long id){
         //2.新增
        userService.removeById(id);
    }

    @ApiOperation("根据id查询用户接口")
    @GetMapping("{id}")
    public UserVO queryUserById(@ApiParam("用户id") @PathVariable("id") Long id){
        User user = userService.getById(id);
        return BeanUtil.copyProperties(user, UserVO.class);
    }

    @ApiOperation("根据id批量查询用户接口")
    @GetMapping
    public List<UserVO> queryUserByIds(@ApiParam("用户id集合") @RequestParam("ids") List<Long> ids){
        List<User> users = userService.listByIds(ids);
        return BeanUtil.copyToList(users, UserVO.class);
    }
    @ApiOperation("扣减用户余额接口")
    @PutMapping("/{id}/deduction/{money}")
    public void deductMoneyById(
            @ApiParam("用户id") @PathVariable("id") Long id,
            @ApiParam("扣减的金额") @PathVariable Integer money){
        userService.deductBalance(id,money);
    }

    @ApiOperation("复杂条件查询用户接口")
    @GetMapping("/list")
    public List<UserVO> queryUsers(UserQuery query){
       List<User> users =  userService.queryUsers(
               query.getName(),query.getStatus(),query.getMinBalance(),query.getMaxBalance()
       );
        return BeanUtil.copyToList(users,UserVO.class);
    }

    @ApiOperation("根据id查询用户以及地址接口")
    @GetMapping("/inAddress/{id}")
    public UserVO queryUserByIdAndAddress(@ApiParam("用户id") @PathVariable("id") Long id){
        return userService.queryUserByIdAndAddress(id);
    }

    @ApiOperation("根据id批量查询用户以及地址接口")
    @GetMapping("/inAddress")
    public List<UserVO> queryUserByIdsAndAddress(@ApiParam("用户id集合") @RequestParam("ids") List<Long> ids){
        return userService.queryUserAndAddressByIds(ids);
    }

    @ApiOperation("根据条件分页查询用户接口")
    @GetMapping("/page")
    public PageDTO<UserVO> queryUsersPage(UserQuery query){
        return userService.queryUsersPage(query);
    }
    @ApiOperation("根据条件分页查询用户接口(封装版)")
    @GetMapping("/pageNew")
    public PageDTO<UserVO> queryUsersPageNew(UserQuery query){
        return userService.queryUsersPageNew(query);
    }
}
